/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hotel.jpa;

import entities.Booking;
import entities.GuestUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author Arb
 */
public class BookingJpaCtrl implements IBookingJpaCtrl<Booking> {

    private final UserTransaction utx;
    private final EntityManagerFactory emf;

    public BookingJpaCtrl(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    @Override
    public void create(Booking t) {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();
        em.persist(t);
        tx.commit();

        em.close();
    }

    @Override
    public void destroy(Long id) {
        EntityManager em = emf.createEntityManager();
        //We need to find right Booking 

        String query = "select t from Booking t";
        Query q = em.createQuery(query);
        List<Booking> result = q.getResultList();
        Booking t = null;
        for (Booking p : result) {
            if (p.getId().compareTo(id) == 0) {
                t = p;
            }
        }
            //Have right booking, now we only have to remove it

        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.remove(t);
        tx.commit();


        em.close();
    }

    @Override
    public void edit(Booking t) {
        
        EntityManager em = emf.createEntityManager();
         //We need to find right Product to edit
        String query = "select b from Booking b";
        Query q = em.createQuery(query);
        List<Booking> result = q.getResultList();
        
        Booking booking = null;
        
        for(Booking b : result){
            if(b.getId().compareTo(t.getId()) == 0){
                booking = b;
            }
        }
        
        booking.setBookDate(t.getBookDate());
        booking.setStartDate(t.getStartDate());
        booking.setEndDate(t.getEndDate());
        booking.setRoom(t.getRoom());
        booking.setUser(t.getUser());
        
        //Have right product, now we only have to merge it
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.merge(booking);
        tx.commit();

        em.close();
    }

    @Override
    public Booking findEntity(Booking t) throws NonexistentEntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Booking> findEntities() {
        EntityManager em = emf.createEntityManager();

        //Find all users 
        String query = "select u from Booking u";
        Query q = em.createQuery(query);
        List<Booking> result = q.getResultList();

        em.close();

        return result;
    }

    @Override
    public EntityManager getEntityManager() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getEntityCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Booking> findMyBookings(GuestUser aUser) {
        EntityManager em = emf.createEntityManager();

        //Find all users 
        String query = "select u from Booking u";
        Query q = em.createQuery(query);
        List<Booking> result = q.getResultList();

        List<Booking> finalList = new ArrayList<Booking>();
        for (Booking b : result) {
            if (b.getUser().equals(aUser)) {
                finalList.add(b);
            }
        }

        em.close();

        return finalList;
    }
}
